package com.sali.service.data_processor.chain.handlers;

import com.alibaba.fastjson.JSONObject;
import com.sali.commons.exception.ProcessChainHandlerInitException;
import com.sali.commons.exception.ProcessChainHandlerProcessException;
import com.sali.entity.ProcessDefinition;
import com.sali.entity.TransferObject;
import com.sali.service.data_processor.chain.IProcessChainHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

public class DataHandler01 implements IProcessChainHandler {

    public static final Logger log = LoggerFactory.getLogger(IProcessChainHandler.class);

    private boolean needCut;

    private Long cutLength;

    @Override
    public void preProcess(ProcessDefinition processDefinition) throws ProcessChainHandlerInitException {
        log.info("data handler 01 init...");
        if (processDefinition.getTextContentExtraction() != null) {
            this.needCut = processDefinition.getTextContentExtraction().isNeedCut();
            this.cutLength = processDefinition.getTextContentExtraction().getCutLength();
        }

    }

    @Override
    public void process(TransferObject input) throws ProcessChainHandlerProcessException {
        log.info("data handler 01 is processing... needCut: {}, cutLength: {}", needCut, cutLength);
        HashMap<String, Object> data = input.getData();

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("dataField", "data 01");

        data.put("data handler 01", jsonObject.toJSONString());
    }

}
